package com.woniu.guoguo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.woniu.guoguo.common.EncryptUtil;
import com.woniu.guoguo.component.ShiroRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Configuration
public class AppConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }


    @Bean
    public CredentialsMatcher hashedCredentialMatcher() {
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(EncryptUtil.ALGORITHM_NAME);
        matcher.setHashIterations(EncryptUtil.HASH_ITERATIONS);
        return matcher;
    }

    //自定义Realm提供数据源
    @Bean
    public Realm shiroRealm() {
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCredentialsMatcher(hashedCredentialMatcher());
        shiroRealm.setCacheManager(cacheManager());
        //认证缓存默认关闭，打开授权缓存
        shiroRealm.setAuthenticationCachingEnabled(true);
        //授权缓存默认开启
        return shiroRealm;
    }

    @Bean
    public MemoryConstrainedCacheManager cacheManager() {
        return new MemoryConstrainedCacheManager();
    }

    //shiro的过滤器
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition sfcd = new DefaultShiroFilterChainDefinition();
        //放行
        sfcd.addPathDefinition("/login","anon");
        sfcd.addPathDefinition("/register","anon");
        sfcd.addPathDefinition("/getMessageByRestaurantId","anon");
        sfcd.addPathDefinition("/getCommentsByPage","anon");
        sfcd.addPathDefinition("/getMenuIdAndCommentsStatus","anon");
        sfcd.addPathDefinition("/getmenu","anon");
        sfcd.addPathDefinition("/getmenuimg","anon");
        sfcd.addPathDefinition("/getmenubyxl","anon");
        sfcd.addPathDefinition("/getmenuimgbyxl","anon");
        sfcd.addPathDefinition("/getmenubyjg","anon");
        sfcd.addPathDefinition("/getmenuimgbyjg","anon");
        sfcd.addPathDefinition("/getmenu2","anon");
        sfcd.addPathDefinition("/getmenuimg2","anon");
        sfcd.addPathDefinition("/getmenubyxl2","anon");
        sfcd.addPathDefinition("/getmenuimgbyxl2","anon");
        sfcd.addPathDefinition("/getmenubyjg2","anon");
        sfcd.addPathDefinition("/getmenuimgbyjg2","anon");
        sfcd.addPathDefinition("/queryMenu","anon");
        sfcd.addPathDefinition("/querybuy","anon");
        sfcd.addPathDefinition("/querysort","anon");
        sfcd.addPathDefinition("/checkLoginStatus","anon");
        sfcd.addPathDefinition("/getMenuId","anon");
        sfcd.addPathDefinition("/getSearchResult","anon");
        sfcd.addPathDefinition("/getType","anon");
        sfcd.addPathDefinition("/getSearchWord","anon");
        sfcd.addPathDefinition("/alipay","anon");
        sfcd.addPathDefinition("/AlipayNotifyUrl","anon");
        sfcd.addPathDefinition("/AplipayReturnUrl","anon");
        sfcd.addPathDefinition("/getSecondsKillActivity","anon");
        sfcd.addPathDefinition("/getAllActiveMenuMessage","anon");

        sfcd.addPathDefinition("/js/**","anon");
        sfcd.addPathDefinition("/img/**","anon");
        sfcd.addPathDefinition("/css/**","anon");
        sfcd.addPathDefinition("/html/shouye.html","anon");
        sfcd.addPathDefinition("/html/MenuManager.html","anon");
        sfcd.addPathDefinition("/html/type.html","anon");
        sfcd.addPathDefinition("/html/search.html","anon");
        sfcd.addPathDefinition("/html/MenuManagerActive.html","anon");

        //logout 登出
        sfcd.addPathDefinition("/logout","anon");
        //需要认证
        sfcd.addPathDefinition("/**","user");

        return sfcd;
    }


    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxUploadSize(50 * 1024 * 1024l);
        commonsMultipartResolver.setMaxUploadSizePerFile(10 * 1024 * 1024l);
        return commonsMultipartResolver;
    }
}
